package com.three.DAO.impl;

import com.three.DAO.BaseDAO;
import com.three.DAO.ConsumInfoDAO;
import com.three.entity.ConsumInfo;
import com.three.entity.MobileCard;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsumInfoDAOImpl extends BaseDAO implements ConsumInfoDAO {

    ResultSet rs=null;

    @Override
    public int addConsumInfo(ConsumInfo consumInfo) {
        String sql="insert into consuminfo (cardNumber,talkTime,smsCount,flow,price)values(?,?,?,?,?)";
        Object[] values={consumInfo.getCardnumber(),consumInfo.getTalkTime(),consumInfo.getSmsCount(),consumInfo.getFlow(),consumInfo.getPrice()};
        int count=super.exeUpdate(sql,values);
        return count;
    }

    @Override
    public List<ConsumInfo> getConsumInfoByNum(MobileCard mobileCard) {
        List<ConsumInfo> list=new ArrayList<>();
        String sql="select * from consuminfo where cardNumber=?";
        ConsumInfo consumInfo;
        Object[] values={mobileCard.getCardNumber()};
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                consumInfo=new ConsumInfo();
                consumInfo.setId(rs.getInt("id"));
                consumInfo.setCardnumber(rs.getString("cardNumber"));
                consumInfo.setTalkTime(rs.getInt("talkTime"));
                consumInfo.setSmsCount(rs.getInt("smsCount"));
                consumInfo.setFlow(rs.getInt("flow"));
                consumInfo.setPrice(rs.getDouble("price"));
                list.add(consumInfo);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if(rs!=null){
                    rs.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public Map<String, List<ConsumInfo>> getAllConsumInfo() {
        List<String> ls=new ArrayList<>();
        String sql="select cardNumber from consuminfo";
        Object[] values={};
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                String cn=rs.getString("cardNumber");
                ls.add(cn);
            }

        }catch (SQLException e){
                e.printStackTrace();
            }finally {
                try{
                    if(rs!=null){
                        rs.close();
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        Map<String, List<ConsumInfo>> map=new HashMap<>();
        for(int i=0;i<ls.size();i++){
            MobileCard mobileCard=new MobileCard();
            mobileCard.setCardNumber(ls.get(i));
            List<ConsumInfo> lc=getConsumInfoByNum(mobileCard);
            if(!map.containsKey(ls.get(i))){
                map.put(ls.get(i),lc);
            }
        }
        return map;
    }
}
